//导入工具类函数
const utils = require('../../utils/util')

//初始化云数据库环境
let db = wx.cloud.database({
    env: 'linyr-6gp7qo8008360a60'
});
//链接云数据库
let pet = db.collection('db_pet')
let look = db.collection('db_look')

// console.log(pet);

Page({

    /**
     * 页面的初始数据
     */
    data: {
        petList: [],
        navList:[
            {
                img:'../../static/image/dog.png',
                title:'犬',
                tid:'狗'
            },
            {
                img:'../../static/image/cat.png',
                title:'猫',
                tid:'猫'
            },
            {
                img:'../../static/image/rabbit.png',
                title:'兔子',
                tid:'兔'
            },
            {
                img:'../../static/image/bird.png',
                title:'鸟',
                tid:'鸟'
            },
            {
                img:'../../static/image/turtle.png',
                title:'龟',
                tid:'龟'
            },
            {
                img:'../../static/image/fish.png',
                title:'鱼',
                tid:'鱼'
            },
            {
                img:'../../static/image/snake.png',
                title:'蛇',
                tid:'蛇'
            },
            {
                img:'../../static/image/spider.png',
                title:'蜘蛛',
                tid:'蜘蛛'
            }
        ],
        navIndex:'狗',
        knowObj:{}
    },

    // 去搜索
    goSearch(){
        wx.navigateTo({
            url: '../search/search',
          })
    },

    //去详情页
    async godetail(event){
        // console.log(event);
        let _id = event.currentTarget.dataset.id
        let dt = new Date()
        let look_time = utils.formatTime(dt)
        await look.where({look_id:_id}).get().then(content=>{
            // console.log(content);
            let { data,errMsg } = content
            if(errMsg=='collection.get:ok'){
                if(data.length==0){
                    pet.where({_id}).get().then(content=>{
                        console.log('2.0',content);
                        let { data,errMsg } = content
                        if(errMsg=='collection.get:ok'){
                            let look_img = data[0].pet_coverURL
                            let look_name = data[0].pet_name
                            look.add({
                                data:{
                                    look_id:_id,
                                    look_time,
                                    look_img,
                                    look_name
                                }
                            })
                        }
                    })
                }
            }
        })
        wx.navigateTo({
          url: '../detail/detail?id='+_id,
        })
    },

    //获取宠物列表
    async listPet() {
        let that = this;
        let count = 0;
        await pet.count().then(content => {
            // console.log(content);
            let {
                total,
                errMsg
            } = content
            if (errMsg == "collection.count:ok") {
                count = total;
            }
        })
        console.log("长度==>", count);
        wx.showLoading({
          title: '加载中...',
          icon:"none"
        })
        let arr = []
        let numlist = []
        for (let i = 0; numlist.length < 6; i++) {
            let num = Math.floor(Math.random() * count)
            if (!numlist.includes(num)) {
                numlist.push(num)
            }
        }
        // console.log("1111===>",numlist);

        for (let j = 0; j < numlist.length; j++) {
            // console.log("随机数==>",num);
            await pet.skip(numlist[j]).limit(1).get().then(content => {
                // console.log(content);
                let {
                    data,
                    errMsg
                } = content
                if (errMsg == 'collection.get:ok') {
                    arr = arr.concat(data)
                    // console.log("数组==>", this.data.petList);
                }
            })

        }
        that.setData({
            petList: arr
        })

        wx.hideLoading()

    },

    // 去宠物小知识页面
    goKnow(event){
        console.log(111);
        let id = event.currentTarget.dataset.id
        wx.navigateTo({
          url: '../knowledge/knowledge?id='+id,
        })
    },

    //获取宠物小知识
    async getKnow() {
        let that = this;
        let count = 0;
        await pet.count().then(content => {
            // console.log(content);
            let {
                total,
                errMsg
            } = content
            if (errMsg == "collection.count:ok") {
                count = total;
            }
        })
        // console.log("长度==>", count);
        let num = Math.floor(Math.random() * count)
        await pet.skip(num).limit(1).get().then(content => {
            // console.log(content);
            let {
                data,
                errMsg
            } = content
            if (errMsg == 'collection.get:ok') {
                if(data[0].pet_feed==''){
                    this.getKnow();
                }else{
                    that.setData({
                        knowObj: data[0]
                    })
                }
                // console.log("数据===>", this.data.knowObj);
            }
        })

    },


    //获取首页宠物列表
    async clickNav(event){
        // console.log(event);
        let that = this;
        if(event.currentTarget.dataset.tid){
            let tid = event.currentTarget.dataset.tid
            this.setData({
                navIndex:tid
            })
            // console.log(this.data.navIndex);
            wx.showLoading({
              title: '加载中...',
              icon:"none"
            })
            await pet.where({pet_tid:this.data.navIndex}).limit(6).get().then(content=>{
                // console.log(content);
                let { data,errMsg } = content
                if(errMsg == 'collection.get:ok'){
                    that.setData({
                        petList:data
                    })
                    // console.log(this.data.petList);
                }
            })
            wx.hideLoading();
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.listPet()
        pet.where({pet_tid:'狗'}).limit(6).get().then(content=>{
            // console.log(content);
            let { data,errMsg } = content
            if(errMsg == 'collection.get:ok'){
                this.setData({
                    petList:data
                })
                // console.log(this.data.petList);
            }
        })
        this.getKnow();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})